package com.yf.dxapp.module.app.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.yf.dxapp.module.app.controller.app.vo.AppUserInfoVo;
import com.yf.dxapp.module.app.dal.dataobject.appuser.AppUserDO;
import com.yf.dxapp.module.app.enums.app.LevelEnum;
import com.yf.dxapp.module.app.service.appuser.AppUserService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author yxmds
 */
@RestController
@RequestMapping("/account/")
public class ApiAppAccountController {

    @Resource
    private AppUserService userService;


    @PostMapping("/info")
    public CommonResult getUserInfo() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null) {
            return error(HttpStatus.UNAUTHORIZED.value(), "账号未登录");
        }
        AppUserDO appUser = userService.getAppUser(userId);

        if (appUser == null) {
            return error(-1, "无效的登录用户，请重新登录");
        }

        AppUserInfoVo userInfoVo = new AppUserInfoVo();
        userInfoVo.setUserId(userId);
        userInfoVo.setAvatar(appUser.getAvatar());
        userInfoVo.setId(appUser.getId());
        userInfoVo.setIsBindMobile(StringUtils.isNotBlank(appUser.getMobile()));
        LevelEnum levelEnum = LevelEnum.levelOf(appUser.getLevelId());
        userInfoVo.setLevel(levelEnum != null ? levelEnum.name : "游客");
        userInfoVo.setMobile(appUser.getMobile());
        userInfoVo.setNickName(appUser.getName());
        userInfoVo.setIsSetPassword(StringUtils.isNotBlank(appUser.getPassword()));

        return success(userInfoVo);
    }
}
